package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;

public class HuaguNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://.*.huagu.com/.*/[0-9]*/[0-9]*[\\-0-9]*.html",
		"http://.*.huagu.com/.*/[0-9]*/.*/[0-9]*.html"
	};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
		NewsMeta news=new NewsMeta();

		String htmltxt=urlMeta.getHtml();
		Document doc=Jsoup.parse(htmltxt);
		doc.select("div.tags,div.page").remove();
		String title=null;
		String content=null;
		Long date=null;

		doc.select("div.newabout").remove();
		doc.select("div.hotdj").remove();
		doc.select("div.anchorsd").remove();
		doc.select("div.left-footer-ad").remove();
		doc.select("div.share-box").remove();
		doc.select("#rightside").remove();


		title=doc.select("h1").text();
		content=HtmlCleaner.getContentHtml(url,doc.select("div.article_con"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.main-article p"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("label#videoDescription"));
			}
		}
		date=DateUtils.matchDate(doc.select("div.info").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("div.inro").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("span#videoTime").text());
			}
		}

		String author=JSoupUtils.matchAuthor(doc, "来源:");
		if ("互联网".equals(author)){
			author=null;
		}
		news.setAuthor(author);
		news.setUrl(url);
		news.setTitle(title);
		news.setContent(content);
		news.setDate(date);
		if (StringUtils.isBlank(news.getTitle())||news.getDate() == null) {
		}
		return news;
	}

	public static void main(String[] args) {
		String url="http://finance.huagu.com/cjyw/1408/278224.html";
		HuaguNewsAnalyse test=new HuaguNewsAnalyse();
		System.out.println(test.isDetailPage(url));
		url=UrlArgumentTop.FromatUrl(url);
		System.out.println(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=test.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
